//
//  MLLoadingScene.m
//  WaterProof
//
//  Created by victor0321 on 12/1/4.
//  Copyright 2012年 __MyCompanyName__. All rights reserved.
//

#import "MLLoadingScene.h"

@interface MLLoadingScene (PrivateMethods)
-(void) update:(ccTime)delta;
@end

@implementation MLLoadingScene

+(id) sceneWithStage:(StageScenes)stageScene;
{
	CCLOG(@"===========================================");
	CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self);
	// This creates an autorelease object of self (the current class: LoadingScene)
	return [[[self alloc] initWithStage:stageScene] autorelease];
	
}

-(id) initWithStage:(StageScenes)stageScene
{
	if ((self = [super init]))
	{
		stage = stageScene;
        
		CCLabelTTF* label = [CCLabelTTF labelWithString:@"Loading ..." fontName:@"Marker Felt" fontSize:64];
		CGSize size = [[CCDirector sharedDirector] winSize];
		label.position = CGPointMake(size.width / 2, size.height / 2);
		[self addChild:label];
		
		// Must wait one frame before loading the target scene!
		// Two reasons: first, it would crash if not. Second, the Loading label wouldn't be displayed.
		[self scheduleUpdate];
	}
	
	return self;
}

-(void) update:(ccTime)delta
{
	// It's not strictly necessary, as we're changing the scene anyway. But just to be safe.
	[self unscheduleAllSelectors];
	
	// Decide which scene to load based on the TargetScenes enum.
	// You could also use TargetScene to load the same with using a variety of transitions.
	switch (stage)
	{
		case Menu:
        {
            CCTransitionFade* transition = [CCTransitionFade transitionWithDuration:1 scene:[MLMenuScene scene] withColor:ccWHITE];
            [[CCDirector sharedDirector] replaceScene:transition];
            break;
        }
        case Stage1:
        {
			CCTransitionFade* transition = [CCTransitionFade transitionWithDuration:1 scene:[MLGameLayer sceneWithStage:Stage1] withColor:ccWHITE];
            [[CCDirector sharedDirector] replaceScene:transition];
            break;
        }
			
		case Stage2:
        {
			CCTransitionFade* transition = [CCTransitionFade transitionWithDuration:1 scene:[MLGameLayer sceneWithStage:Stage2] withColor:ccWHITE];
            [[CCDirector sharedDirector] replaceScene:transition];
            break;
        }
		case Stage3:
        {
			CCTransitionFade* transition = [CCTransitionFade transitionWithDuration:1 scene:[MLGameLayer sceneWithStage:Stage3] withColor:ccWHITE];
            [[CCDirector sharedDirector] replaceScene:transition];
            break;
        }
        case Stage4:
        {
			CCTransitionFade* transition = [CCTransitionFade transitionWithDuration:1 scene:[MLGameLayer sceneWithStage:Stage4] withColor:ccWHITE];
            [[CCDirector sharedDirector] replaceScene:transition];
            break;
        }
        case Stage5:
        {
			CCTransitionFade* transition = [CCTransitionFade transitionWithDuration:1 scene:[MLGameLayer sceneWithStage:Stage5] withColor:ccWHITE];
            [[CCDirector sharedDirector] replaceScene:transition];
            break;
        }
        case Stage6:
        {
			CCTransitionFade* transition = [CCTransitionFade transitionWithDuration:1 scene:[MLGameLayer sceneWithStage:Stage6] withColor:ccWHITE];
            [[CCDirector sharedDirector] replaceScene:transition];
            break;
        }
		default:
			// Always warn if an unspecified enum value was used. It's a reminder for yourself to update the switch
			// whenever you add more enum values.
			NSAssert2(nil, @"%@: unsupported stageScene %i", NSStringFromSelector(_cmd), stage);
			break;
	}
	
}

-(void) dealloc
{
	CCLOG(@"===========================================");
    CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self);
	
	// don't forget to call "super dealloc"
	[super dealloc];
}


@end
